package com.useeinfo.oa.modules.wuHanCommon.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.Warning;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("WarningDao")
public class WarningDao implements CrudDao<Warning> {
    private final static Logger logger = LoggerFactory.getLogger(WarningDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String otherWarningId = queryHash.get("otherWarningId");
        if (StringUtils.isNotBlank(otherWarningId)) {
            conditionHash.put("otherWarningId = ?{paramIndex} ", otherWarningId);
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(w) from Warning w ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<Warning> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select w from Warning w ", "order by w.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<Warning> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, Warning.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public Warning findModel(Long id) {
        return entityManager.find(Warning.class, id);
    }

    @Override
    public Integer add(Warning model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("WarningDaoImpl添加Warning成功！");
        return 1;
    }

    @Override
    public Integer update(Warning model) {
        Warning warning = entityManager.find(Warning.class, model.getId());
        warning.setUpdateDate(new Date());
        warning.setWarningGroup(model.getWarningGroup());
        warning.setOtherWarningId(model.getOtherWarningId());
        warning.setOrganization(model.getOrganization());
        warning.setWarningStatus(model.getWarningStatus());
        warning.setSystem(model.getSystem());
        warning.setStartTime(model.getStartTime());
        warning.setLevel(model.getLevel());
        warning.setContent(model.getContent());
        warning.setLastSeconds(model.getLastSeconds());
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        Warning warning = entityManager.find(Warning.class, id);
        entityManager.remove(warning);
        return 1;
    }

    @Override
    public Integer deleteLogic(Warning model) {
        return null;
    }
}
